package com.gary.spring.proxy;

import java.lang.reflect.Method;
import java.lang.reflect.UndeclaredThrowableException;

/**
 *
 * 模拟jdk生成的代理类
 *  使用invocationHandler 抽离方法的具体实现，将实现逻辑交给程序员
 * @author peng_rui
 * @date 2022/7/3
 */
public class $JdkProxy0 implements JdkProxyDeep.Foo {

    private JdkProxyDeep.InvocationHandler invocationHandler;

    public $JdkProxy0(JdkProxyDeep.InvocationHandler invocationHandler) {
        this.invocationHandler = invocationHandler;
    }
    @Override
    public void foo() {
        try {
            Method method = JdkProxyDeep.Foo.class.getMethod("foo");
            invocationHandler.invoke(this, method, null);
        } catch (RuntimeException | Error e) {
            throw e;
        } catch (Throwable t) {
            throw  new UndeclaredThrowableException(t);
        }
    }

    @Override
    public Integer bra() {
        try {
            Method method = JdkProxyDeep.Foo.class.getMethod("bra");
            Object invoke = invocationHandler.invoke(this, method, null);
            return (Integer) invoke;
        } catch (RuntimeException | Error e) {
            throw e;
        } catch (Throwable t) {
            throw  new UndeclaredThrowableException(t);
        }
    }
}
